/**
 * @Description:配置部门根节点
 * @Author: CHEHSHUANG
 * @Date: 2019/1/23
 */
import React, {Component} from 'react';
import { Form, Input, Row, Button, Col, message} from "antd";
import { saveTenantAdmin} from "./TenantService";

const FormItem = Form.Item;
const formLayout = {
    labelCol: {span: 6},
    wrapperCol: {span: 16}
}
class ConfigDepartmentRoot extends Component {
    state = {
        loading: false
    }
    handleSave = () => {
        let result = false;
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                result = values;
            }
        });
        if(result){
            const {tenantAdmin,tenantCode} = this.props;
            let saveData;
            saveData = {...tenantAdmin, ...result,tenantCode,createAdmin: true};
            this.setState({loading: true});
            saveTenantAdmin(saveData).then(res => {
                if(res.status.toLowerCase()==="success"){
                    message.success(res.message);
                }else{
                    message.error(res.message);
                }
            }).catch(e => {
            }).finally(() => {
                this.setState({loading: false});
            })
        }
    }
    render() {
        const {getFieldDecorator} = this.props.form;
        let {tenantAdmin,departmentRoot}  = this.props;
        if(!tenantAdmin&&departmentRoot){
          tenantAdmin = {organization:{id:departmentRoot.id,name:departmentRoot.name}};
        }
        const {loading}  = this.state;
        return (
            <Form>
                <FormItem
                  style={{display:"none"}}
                >
                    {getFieldDecorator('organization.id', {
                        initialValue: tenantAdmin && tenantAdmin.organization ? tenantAdmin.organization.id : "",
                    })(
                        <Input disabled/>
                    )}
                </FormItem>
              <Row>
                <FormItem
                  label={"组织机构"}
                  {...formLayout}
                >
                  {getFieldDecorator('organization.name', {
                    initialValue: tenantAdmin && tenantAdmin.organization ? tenantAdmin.organization.name : "",
                    rules: [{required: false}]
                  })(
                    <Input disabled/>
                  )}
                </FormItem>
              </Row>
              <Row>
                <FormItem
                  label={"员工编号"}
                  {...formLayout}
                >
                  {getFieldDecorator('code', {
                    initialValue: tenantAdmin && tenantAdmin.code ? tenantAdmin.code : "",
                    rules: [{required: true, message: '请填写员工编号!'}]
                  })(
                    <Input placeholder={"请填写员工编号"}/>
                  )}
                </FormItem>
              </Row>
              <Row>
                <FormItem
                  label={"姓名"}
                  {...formLayout}
                >
                  {getFieldDecorator('userName', {
                    initialValue: tenantAdmin && tenantAdmin.userName ? tenantAdmin.userName : "",
                    rules: [{required: true, message: '请填写姓名!'}]
                  })(
                    <Input placeholder={"请填写姓名"}/>
                  )}
                </FormItem>
              </Row>
              <Row>
                <FormItem
                  label={"邮箱"}
                  {...formLayout}
                >
                  {getFieldDecorator('email', {
                    initialValue: tenantAdmin && tenantAdmin.email ? tenantAdmin.email : "",
                    rules: [{type:"email",required: true, message: '请填写邮箱!'}]
                  })(
                    <Input placeholder={"请填写邮箱"}/>
                  )}
                </FormItem>
              </Row>
                <br/>
                <Row type={"flex"} justify={"center"} style={{marginTop: 20}}>
                    <Button
                        type={"primary"}
                        loading={loading}
                        onClick={this.handleSave}
                    >
                        保存
                    </Button>
                </Row>
            </Form>
        );
    }
}
export default Form.create()(ConfigDepartmentRoot);
